package com.ruoyi.project.modules.pageImageElementRef.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.project.modules.pageElement.domain.PageElement;
import com.ruoyi.project.modules.pageElement.service.IPageElementService;
import com.ruoyi.project.modules.pageImageElementCondition.domain.PageImageElementCondition;
import com.ruoyi.project.modules.pageImageElementCondition.service.IPageImageElementConditionService;
import com.ruoyi.project.modules.pageImageElementDetail.domain.PageImageElementConditionDetail;
import com.ruoyi.project.modules.pageImageElementDetail.service.IPageImageElementConditionDetailService;
import com.ruoyi.project.modules.processBranch.service.IProcessBranchService;
import com.ruoyi.project.modules.processBranch.vo.ParamObject;
import lombok.AllArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.project.modules.pageImageElementRef.domain.PageImageElementRef;
import com.ruoyi.project.modules.pageImageElementRef.service.IPageImageElementRefService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 页面图片与元素关系Controller
 *
 * @author smallrain
 * @date 2022-05-06
 */
@Controller
@RequestMapping("/pageImageElementRef/pageImageElementRef")
// @AllArgsConstructor
public class PageImageElementRefController extends BaseController {
    private String prefix = "pageImageElementRef";

    @Autowired
    private IPageImageElementRefService pageImageElementRefService;

    @Autowired
    private IPageElementService pageElementService;

    @Autowired
    private IPageImageElementConditionService pageImageElementConditionService;

    @Autowired
    private IPageImageElementConditionDetailService pageImageElementConditionDetailService;

    @Autowired
    private IProcessBranchService processBranchService;

    @GetMapping("/{pageId}/{pageImageId}")
    public String pageImageElementRef(@PathVariable("pageId") Long pageId,
                                      @PathVariable("pageImageId") Long pageImageId,
                                      @RequestParam(value = "pageName", required = false) String pageName,
                                      ModelMap mmap)
    {
        mmap.put("pageId", pageId);
        mmap.put("pageImageId", pageImageId);
        mmap.put("pageName", pageName);
        return prefix + "/pageImageElementRef";
    }
    @GetMapping("/addPageElement/{pageId}/{pageImageId}")
    public String addPageElement(@PathVariable("pageId") Long pageId,
                                 @PathVariable("pageImageId") Long pageImageId,
                                 ModelMap mmap)
    {
        mmap.put("pageId", pageId);
        mmap.put("pageImageId", pageImageId);
        return prefix + "/addPageElement";
    }
    /**
     * 查询页面元素列表
     * @return
     */
    @PostMapping("/queryPageElement")
    @ResponseBody
    public AjaxResult queryPageElement() {
        List<PageElement> pageElements = this.pageElementService.selectPageElementList(new PageElement());
        return AjaxResult.success(pageElements);
    }

    /**
     * 查询页面图片与元素关系列表
     * eg:
     * {
     *      pageId:1,//页面id
     *      pageImageId: 1// 页面图片id
     * }
     */
    @PostMapping("/list")
    @ResponseBody
    public AjaxResult list(PageImageElementRef pageImageElementRef) {
        List<PageImageElementRef> list = pageImageElementRefService.selectPageImageElementRefList(pageImageElementRef);
        return AjaxResult.success(list);
    }

    /**
     * 保存页面图片与元素关系
     * eg：
     * {
     *      pageId:1,//页面id
     *      pageImageId: 1,// 页面图片id
     *      pageElementIds:[ // 页面元素id
     *          1,2,3
     *      ]
     * }
     */
    @Log(title = "页面图片与元素关系", businessType = BusinessType.UPDATE)
    @PostMapping("/save")
    @ResponseBody
    public AjaxResult save(@RequestBody PageImageElementRef pageImageElementRef) {
        return toAjax(pageImageElementRefService.updatePageImageElementRef(pageImageElementRef));
    }

    /**
     * 查询页面图片与元素 条件列表
     * eg:
     * {
     *      pageId:1,//页面id
     *      pageImageId: 1// 页面图片id
     * }
     * return:
     * [{
     *     pageElementName:"页面头部",
     *     pageImageElementConditionList:[{
     *         id:1,
     *         name : "不展示",
     *         description:'xxx'
     *     },{
     *         id:2
     *         name : "展示",
     *         description:'xxx'
     *     }]
     * }]
     */
    @PostMapping("/conditionList")
    @ResponseBody
    public AjaxResult conditionList(@RequestBody PageImageElementRef pageImageElementRef) {
        List<PageImageElementRef> list = pageImageElementRefService.conditionList(pageImageElementRef);
        return AjaxResult.success(list);
    }

    /**
     * 查询条件明细
     * @author chenxiaoyu
     * @date 2022/5/7 下午10:08
     * @param conditionId 条件id
     * @return com.ruoyi.common.core.domain.AjaxResult
     */
    @PostMapping("/conditionDetailList")
    @ResponseBody
    public AjaxResult conditionDetailList(Long conditionId) {
        PageImageElementCondition pageImageElementCondition = this.pageImageElementConditionService.selectPageImageElementConditionById(conditionId);
        PageImageElementConditionDetail pageImageElementConditionDetail = new PageImageElementConditionDetail();
        pageImageElementConditionDetail.setConditionId(conditionId);
        List<PageImageElementConditionDetail> pageImageElementConditionDetails = pageImageElementConditionDetailService.selectPageImageElementConditionDetailList(pageImageElementConditionDetail);
        pageImageElementCondition.setPageImageElementConditionDetailList(pageImageElementConditionDetails);
        return AjaxResult.success(pageImageElementCondition);
    }

    /**
     * 根据接口id，查询字段/数据集集合
     * @author chenxiaoyu
     * @date 2022/4/3 上午12:27
     * @param interfaceId 接口id
     * @param type 1-字段 2-数据集
     * @return com.ruoyi.common.core.domain.AjaxResult
     */
    @GetMapping("/queryFieldList")
    @ResponseBody
    public AjaxResult queryParamList(Long interfaceId, String type) {
        List<ParamObject> paramObjectList = this.processBranchService.paramObjectList(
                "2", Arrays.asList(interfaceId), "2", type);
        return AjaxResult.success(paramObjectList);
    }

    /**
     * 根据数据集id，查询数据集字段集合
     * @author chenxiaoyu
     * @date 2022/4/3 上午12:27
     * @param id 数据集id
     * @return com.ruoyi.common.core.domain.AjaxResult
     */
    @GetMapping("/queryParamFieldList")
    @ResponseBody
    public AjaxResult queryParamFieldList(Long id) {
        List<ParamObject> paramObjectList = this.processBranchService.queryParamFieldList(id);
        return AjaxResult.success(paramObjectList);
    }


    /**
     * 保存页面图片与元素 条件列表
     * eg:
     * {
     *      id:1,// 更新时必填
     *      pagImageElementRefId:1,//
     *      elementConditionType:1,// 元素条件类型:1-展现 2-不展现 3-禁用
     *      joinCondition: and ,// 关联条件（AND，OR）
     *      description: xxx, // 条件描述
     *      pageImageElementConditionDetailList:[{
     *             branchConditionDetailDictCode: '1',//分支条件模式字典数据ID (1-字段 2-数据集条数)
     *             branchConditionGroupId:'1',//分支条件字典数据分组(1-组1 .. n-组n)
     *             objectCategory:'1',// 对象分类（1-字段，2-数据集，3-出参）
     *             objectType: '1', // 对象类型（1-字段，2-数据集，3-数据集字段）
     *             objectId:111,// 字段ID/数据集ID/数据集字段id
     *             paramsId:1,//数据集id，当对象类型为3 时，必填
     *             interfaceId:1,// 接口id
     *             interfaceName:"",//接口名称
     *             interfaceUrl:"",//接口名称
     *             objectName: "名称", // 对象名称(字段或者数据集)
     *             objectEnName: "英文", // 对象英文名称(字段或者数据集)
     *             objectFieldName: "对象字段名称", // 对象字段名称(数据集字段)
     *             objectFieldEnName: "对象字段英文名称", // 对象字段英文名称(数据集字段)
     *             judgeConditionDictCode:'1', //判断条件字典
     *             contractValue:'admin' //约定值
     *             dictName:'是', // 字典名称
     *             joinCondition: 'and', //分组中 条件 and / or
     *             sortNum:1 //序号
     *      }]
     * }
     */
    @PostMapping("/saveCondition")
    @ResponseBody
    public AjaxResult saveCondition(@RequestBody PageImageElementCondition pageImageElementCondition) {
        this.pageImageElementConditionService.saveCondition(pageImageElementCondition);
        return success();
    }

    /**
     * 删除条件
     * @author chenxiaoyu
     * @date 2022/5/7 下午10:44
     * @param conditionId 条件id
     * @return com.ruoyi.common.core.domain.AjaxResult
     */
    @PostMapping("/deleteCondition")
    @ResponseBody
    public AjaxResult deleteCondition(Long conditionId) {
        this.pageImageElementConditionService.deletePageImageElementConditionById(conditionId);
        return success();
    }
}
